#include "ReadXls.h"

ReadXls::ReadXls(QString xlsName, QObject *parent):QObject(parent),mXlsObj(0),mWorkBooks(0),mWorkBook(0),mSheets(0)
{
    mXlsObj = new QAxObject("Excel.Application");
    mWorkBooks = mXlsObj->querySubObject("Workbooks");//Get the pointer of the set of Workbooks
    if(0 == mWorkBooks)
        return;

    mWorkBook = mWorkBooks->querySubObject("Open(const QString&)",xlsName);//Open xls

    #ifdef DBUG
        qDebug() << "mWorkBook";
    #endif

    if(0 == mWorkBook)
        return;

    mSheets = mWorkBook->querySubObject("Sheets"); //Get the pointer of Sheets
    if(0 == mSheets)
        return;    
    int iSheetCount = mSheets->property("Count").toInt();
    for(int index = 1; index <= iSheetCount; ++index)
    {
        QAxObject *sheetObj = mWorkBook->querySubObject("Worksheets(int)",index);
        if(0 == sheetObj)
        {
            Clear();
            return;
        }
        mListSheet.append(XlsSheet(sheetObj));
    }
}

ReadXls::~ReadXls()
{
    Clear();
}

void ReadXls::Clear()
{
    mListSheet.clear();
    mWorkBook->dynamicCall("Close (Boolean)",false);
    mXlsObj->dynamicCall("Quit(void)");

    mSheets = 0;
    mWorkBook = 0;
    mWorkBooks = 0;

    delete mXlsObj;
    mXlsObj = 0;
}

int ReadXls::GetXlsSheetCount() const
{
    return mListSheet.size();
}

int ReadXls::GetSheetUseColStart(int sheetIndex) const
{
    if(sheetIndex < 1 || sheetIndex > mListSheet.size())
        return -1;
    return mListSheet[sheetIndex-1].mStartCol;
}

int ReadXls::GetSheetUseColCount(int sheetIndex) const
{
    if(sheetIndex < 1 || sheetIndex > mListSheet.size())
        return -1;
    return mListSheet[sheetIndex-1].mColCount;
}

int ReadXls::GetSheetUseRowStart(int sheetIndex) const
{
    if(sheetIndex < 1 || sheetIndex > mListSheet.size())
        return -1;
    return mListSheet[sheetIndex-1].mStartRow;
}

int ReadXls::GetSheetUseRowCount(int sheetIndex) const
{
    if(sheetIndex < 1 || sheetIndex > mListSheet.size())
        return -1;
    return mListSheet[sheetIndex-1].mRowCount;
}

QString ReadXls::GetData(int sheetIndex, int row, int col)
{
    if(sheetIndex < 1 || sheetIndex > mListSheet.size())
        return QString();
    XlsSheet sh = mListSheet[sheetIndex-1];

    if(row < sh.mStartRow || row >= sh.mStartRow + sh.mRowCount)
        return QString();
    if(col < sh.mStartCol || col >= sh.mStartCol + sh.mColCount)
        return QString();
    QAxObject *sheetObj = mWorkBook->querySubObject("Worksheets(int)",sheetIndex);
    if(0 == sheetObj)
        return QString();   
    QAxObject *range = sheetObj->querySubObject("Cells(int,int)",row,col);
    if(0 == range)
        return QString();
    QVariant var = range->property("Value");
    if(var.type() == QVariant::Invalid)
        return QString();
    return var.toString();
}

bool ReadXls::StoreDataShown(DataTobeShown &dataTobeShwon, int sheetIndex, QString type)
{
    int rowStart = GetSheetUseRowStart(sheetIndex);
    int rowCount = GetSheetUseRowCount(sheetIndex);
    dataTobeShwon.valueMax = dataTobeShwon.valueMin = GetData(sheetIndex,rowStart + 1, 3).toDouble();


    for(int index = 0; index < rowCount - 1; index ++)
    {
        double tmpData = GetData(sheetIndex,rowStart + index + 1, 3).toDouble();
        dataTobeShwon.dataShown.append(tmpData);
        if(tmpData < dataTobeShwon.valueMin)
            dataTobeShwon.valueMin = tmpData;
        if(tmpData > dataTobeShwon.valueMax)
            dataTobeShwon.valueMax = tmpData;
    }

    return true;
}
